import { Component, OnInit } from '@angular/core';

import { LockScreenState, LockScreenService } from '@core/base';
import { NzModalService } from 'ng-zorro-antd/modal';

import { LockComponent } from './component/lock.component';
import { UnlockComponent } from './component/unlock.component';

@Component({
  selector: 'app-lock-screen',
  templateUrl: './lock-screen.component.html',
  styleUrls: ['./lock-screen.component.less']
})
export class LockScreenComponent implements OnInit {
  max_counter = 3;
  counter = this.max_counter;
  lockScreenState: LockScreenState = { locked: false };

  constructor(private modalSrc: NzModalService, private lockScreenSrc: LockScreenService) {}

  ngOnInit(): void {
    this.lockScreenSrc.lockScreenStateChanged$.subscribe(data => (this.lockScreenState = data));
    if (!this.lockScreenState.locked) {
      this.modalSrc.create({
        nzTitle: '系统锁定',
        nzContent: LockComponent,
        nzMaskClosable: false,
        nzClosable: false,
        nzFooter: null
      });
    }
  }

  onUnlock(): void {
    if (this.counter <= 0) {
      return;
    }
    const modal = this.modalSrc.create({
      nzTitle: '解锁系统',
      nzContent: UnlockComponent,
      nzMaskClosable: false,
      nzClosable: false,
      nzFooter: null
    });

    modal.afterClose.subscribe(() => (this.counter -= 1));
  }

  onReLogin(): void {
    // onReLogin
  }
}
